package com.lingfengerick.commons.date;

import com.lingfengerick.commons.constant.DateFormatConstant;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 基于java8的时间工具类
 * @author lingfengerick
 * @version 1.0
 * @since 1.8+
 */
public class LocalDateUtils {

    /**
     * 获取当前时间精确到天
     * @return 字符类型时间   yyyy-MM-dd
     */
    public static String getCurrentDate(){
        return LocalDate.now().toString();
    }

    /**
     * 获取当前年
     * @return 年
     */
    public static int getCurrentYear(){
        return LocalDate.now().getYear();
    }

    /**
     * 获取当前月份
     * @return 月
     */
    public static int getCurrentMonths(){
        return LocalDate.now().getMonthValue();
    }

    /**
     * 获取当前月份中的日期
     * @return 月份中日期
     */
    public static int getCurrentDayByMonth(){
        return LocalDate.now().getDayOfMonth();
    }

    /**
     * 获取本周内的时间，即星期几
     * @return 本周内的时间
     */
    public static int getCurrentDayByWeek(){
        return LocalDate.now().getDayOfWeek().getValue();
    }

    /**
     * 获取精确到秒的当前时间
     * @return 当时时间
     */
    public static String getCurrentTimeExactSec(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_EXACTSEC);
        return LocalDateTime.now().format(dateTimeFormatter);
    }

    /**
     * 计算两个日期差
     * @author  lingfengerick
     * @since   2020/2/11 8:01 下午
     * @param startTime 开始时间  yyyy-MM-dd HH:mm:ss
     * @param endTime 结束时间 yyyy-MM-dd HH:mm:ss
     * @return 相差秒数
     *
     */
    public static int betweenSecond(String startTime , String endTime){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_EXACTSEC);
        LocalDateTime startDateTime = LocalDateTime.parse(startTime , dateTimeFormatter);
        LocalDateTime endDateTime = LocalDateTime.parse(endTime , dateTimeFormatter);
        return (int) Duration.between(startDateTime , endDateTime).toMillis()/(1000);
    }

    /**
     * 计算两个时间差
     * @author  lingfengerick
     * @since   2020/2/17 11:28 下午
     * @param startTime 开始时间 HH:mm:ss
     * @param endTime 结束时间 HH:mm:ss
     * @return  相差秒数
     *
     */
    public static int betweenTimeSecond(String startTime , String endTime){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_EXACTTIME);
        LocalTime startDateTime = LocalTime.parse(startTime , dateTimeFormatter);
        LocalTime endDateTime = LocalTime.parse(endTime , dateTimeFormatter);
        return (int)Duration.between(startDateTime , endDateTime).toMillis()/1000;
    }


    /**
     * 获取时间对应的秒
     * @author  lingfengerick
     * @since  2020/2/17 10:12 上午
     * @param time 要解析的时间 格式为： HH:mm:ss
     * @return  解析的秒
     * @throws Exception 异常
     */
    public static int parseSecondLocalDateByTime(String time) throws Exception{
        int resultSecond = 0;
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_EXACTTIME);
            LocalTime localTime = LocalTime.parse(time , dateTimeFormatter);
            int temHour = localTime.getHour();
            int temMinuts = localTime.getMinute();
            int temSecond = localTime.getSecond();
            resultSecond = temHour * 3600 + temMinuts * 60 +temSecond;
        }catch (Exception e){
            throw e;
        }
        return resultSecond;
    }
    /**
     * 将秒转为分钟秒的格式
     * @author  lingfengerick
     * @since   2020/4/5 10:11 下午
     * @param  second 秒
     * @return  分钟秒格式：MM:ss  10:00
     *
     */
    public static String parseSecondToTime(long second){
        String time = "";
        try {
            int minute = (int)second / 60;
            int secondInt = (int)second % 60;
            String secondStr = String.valueOf(secondInt);
            if (secondInt < 10 ){
                secondStr = "0" + secondStr;
            }
            time =  minute + ":" + secondStr;
        }catch (Exception e){
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将秒转为时分秒的格式
     * @author  lingfengerick
     * @since   2020/4/5 10:11 下午
     * @param  second 秒
     * @return  分钟秒格式：hh:MM:ss  00:10:00
     *
     */
    public static String parseHourToTime(long second){
        String time = "";
        try {
            int hour = (int)second / 3600;
            String hourStr = String.valueOf(hour);
            int minute = ((int)second / 60) % 60;
            String minuteStr = String.valueOf(minute);
            int secondInt = (int)second % 60;
            String secondStr = String.valueOf(secondInt);
            //对小时小于10的添加0
            if (hour < 10){
                hourStr = "0" + hourStr;
            }
            if (minute < 10){
                minuteStr = "0" + minuteStr;
            }
            if (secondInt < 10 ){
                secondStr = "0" + secondStr;
            }
            time =  hourStr + ":" + minuteStr + ":" + secondStr;
        }catch (Exception e){
            e.printStackTrace();
        }
        return time;
    }
    /**
     * 比较两个时间
     * @author  lingfengerick
     * @since  2020/08/01 8:01 下午
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 前者比后者大则返回正，否则返回负数
     *
     */
    public static int compareTime(String startTime , String endTime){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_EXACTSEC);
        LocalDateTime startDateTime = LocalDateTime.parse(startTime , dateTimeFormatter);
        LocalDateTime endDateTime = LocalDateTime.parse(endTime , dateTimeFormatter);
        return startDateTime.compareTo(endDateTime);
    }


    /**
     * 转换时间格式
     * @author  lingfengerick
     * @since   2020/8/9 3:45 下午
     * @param  formatTime 需要转换时间
     * @return  转换后的时间
     *
     */
    public static String formatDate(String formatTime){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_EXACTSEC);
        LocalDateTime localTime = LocalDateTime.parse(formatTime , dateTimeFormatter);
        DateTimeFormatter toDateTimeFormatter = DateTimeFormatter.ofPattern(DateFormatConstant.DATE_FORMAT_SLASH);
        return localTime.format(toDateTimeFormatter);
    }
}
